package com.doublefish.doublefishrobot.ui.home;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.ui.home.widget.CustomNameDialog;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.MessageEvent;
import com.doublefish.doublefishrobot.utils.ble.DFBleManager;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;
import com.doublefish.doublefishrobot.utils.db.DataBaseApi;
import com.doublefish.doublefishrobot.widget.CommonDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/11/6 14:22
 */
public class DeviceInfoActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView titleTV;

    @BindView(R.id.tv_device_name)
    TextView deviceNameTV;
    @BindView(R.id.tv_device_type)
    TextView deviceTypeTV;
    @BindView(R.id.tv_connect_type)
    TextView connectTypeTV;

    @BindView(R.id.ll_init_check_wrap)
    LinearLayout initWrapLL;
    @BindView(R.id.ll_firmware_wrap)
    LinearLayout firmwareLL;

    private DeviceBean deviceBean;

    public static Intent makeIntent(Activity activity, DeviceBean deviceBean) {
        AppCacheUtil.getInstance().setIntentBean(deviceBean);

        Intent intent = new Intent(activity, DeviceInfoActivity.class);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_info;
    }

    @Override
    protected void initView() {
        titleTV.setText(R.string.device_info);
    }

    @Override
    protected void initData() {
        deviceBean = AppCacheUtil.getInstance().getIntentBean();

        deviceNameTV.setText(deviceBean.getAlias());
        deviceTypeTV.setText(deviceBean.getDeviceTypeName());

        if (deviceBean.getDeviceType() == ModeConstant.DeviceTypeS8 || deviceBean.getDeviceType() == ModeConstant.DeviceTypeS6) {
            //超6 超8
            connectTypeTV.setText(getString(R.string.bluetooth) +" | " + deviceBean.getName());
        } else {
            //超5
            connectTypeTV.setText("WIFI | " + deviceBean.getName());
            initWrapLL.setVisibility(View.GONE);
            firmwareLL.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.ll_device_name, R.id.ll_device_type, R.id.ll_connect_type, R.id.ll_init_check, R.id.ll_firmware })
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.ll_device_name: {
                editName();
            }
            break;

            case R.id.ll_device_type: {

            }
            break;

            case R.id.ll_connect_type: {

            }
            break;

            case R.id.ll_init_check: {
                willToInitCheckActivity();
            }
            break;

            case R.id.ll_firmware: {
                willToFirmwareActivity();
            }
            break;
        }
    }

    private void editName() {
        CustomNameDialog dialog = new CustomNameDialog(this, new CustomNameDialog.OnDialogListner() {
            @Override
            public void onComfirmBtnClick(String text) {
                updateDeviceName(text);
                hideKeyboard();
            }

            @Override
            public void onCancelBtnClick() {
                hideKeyboard();
            }
        });
        dialog.setTitle(R.string.update_device_name);
        dialog.setName(deviceBean.getAlias());
        dialog.show();
    }
    private void updateDeviceName(String alis) {
        if (TextUtils.isEmpty(alis)) {
            return;
        }
        deviceBean.setAlias(alis);
        DataBaseApi.saveDeviceInfo(deviceBean, new DataBaseApi.OnDatabaseCommonListner() {
            @Override
            public void onCallback(boolean suc) {
                if (suc) {
                    deviceNameTV.setText(alis);
                    EventBus.getDefault().post(new MessageEvent(MessageEvent.BIND_DEVICES_CHANGED_NOTI, null));
                } else {
                    showToast(getString(R.string.fail_update));
                }
            }
        });
    }

    private void willToInitCheckActivity() {
        //1.没有连接发球机
        if (!DFBleManager.getInstance().isDidConnectBlueTooth()) {
            toInitCheckActivity();
            return;
        }
        //2.已经连接发球机
        //2.1 连接的是当前发球机
        if (DFBleManager.getInstance().getBleDeviceBean().isSameDevice(deviceBean)) {
            toInitCheckActivity();
            return;
        }
        //2.2 连接了其他设备
        //询问连接新设备
        String title = getString(R.string.ble_connect_alert) + " '" + DFBleManager.getInstance().getBleDeviceBean().getAlias() + "'";
        String msg = getString(R.string.ble_connect_alert_2);
        CommonDialog dialog = new CommonDialog(this, title, msg, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    DFBleManager.getInstance().disConnect();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.disconnect));
        dialog.show();
    }

    private void toInitCheckActivity() {
        Intent intent = DeviceCheckActivity.makeIntent(this, deviceBean);
        startActivity(intent);
    }

    private void willToFirmwareActivity() {
        //1.没有连接发球机
        if (!DFBleManager.getInstance().isDidConnectBlueTooth()) {
            toFirmwareActivity();
            return;
        }
        //2.已经连接发球机
        //2.1 连接的是当前发球机
        if (DFBleManager.getInstance().getBleDeviceBean().isSameDevice(deviceBean)) {
            toFirmwareActivity();
            return;
        }
        //2.2 连接了其他设备
        //询问连接新设备
        String title = getString(R.string.ble_connect_alert) + " '"  + DFBleManager.getInstance().getBleDeviceBean().getAlias() + "'";
        String msg = getString(R.string.ble_connect_alert_2);
        CommonDialog dialog = new CommonDialog(this, title, msg, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    DFBleManager.getInstance().disConnect();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.disconnect));
        dialog.show();
    }

    private void toFirmwareActivity() {
        Intent intent = DeviceFirmwareActivity.makeIntent(this, deviceBean);
        startActivity(intent);
    }

    @OnClick(R.id.tv_unbind)
    public void unBindClick() {
        CommonDialog commonDialog = new CommonDialog(this, getString(R.string.unbind_alert), getString(R.string.unbind_alert_2), new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    preUnBind();
                }
            }
        });
        commonDialog.show();
    }

    private void preUnBind() {
        boolean isDidConnectBlueTooth = DFBleManager.getInstance().isDidConnectBlueTooth();
        //1.没有连接发球机
        if (isDidConnectBlueTooth) {
            unBind();
            return;
        }
        //2.已经连接发球机
        //2.1 连接的是当前发球机
        DeviceBean current = DFBleManager.getInstance().getBleDeviceBean();
        if (this.deviceBean.isSameDevice(current)) {
            DFBleManager.getInstance().disConnect();
        }
        unBind();
    }

    private void unBind() {
        DataBaseApi.deleteDeviceInfo(this.deviceBean, new DataBaseApi.OnDatabaseCommonListner() {
            @Override
            public void onCallback(boolean suc) {
                if (suc) {
                    showToast(getString(R.string.success_unbind));
                    EventBus.getDefault().post(new MessageEvent(MessageEvent.BIND_DEVICES_CHANGED_NOTI, null));
                    finish();
                } else {
                    showToast(getString(R.string.fail_unbind));
                }
            }
        });
    }

    @OnClick(R.id.ll_back)
    public void onBackClick() {
        finish();
    }


    //回收键盘
    private void hideKeyboard() {
        //回收键盘
        InputMethodManager imManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imManager != null && getCurrentFocus() != null) {
            imManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }
}
